<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'角色列表'),
	array('op'=>'edit','title'=>'添加角色'),	
);
if($_RQ['op']=='list'){
	$params=array();
$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}

	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','storeid','status');
	$list=pdo_getslice('jy_weishop_store_role',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['store']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		}
		}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'角色名称','type'=>'string'),
			array('name'=>'store','title'=>'所属门店','type'=>'string'),			
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'name'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_store_role',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$menu=$this->menustore();
	foreach($menu as $v){
		$k=$v['url'];
		$return['perm1'][$k]=$v['title'];
		foreach($v['child'] as $c){
			foreach($c['child'] as $x){
			$e=$x['url'];
			$return['perm2'][$e]=$x['title'];
		}
		}
	}
	$return['perm_mobile']=array(
		'jy_weishop/store.manage.order/'=>'订单管理',
		'jy_weishop/store.manage.goods/'=>'商品管理',
		'jy_weishop/store.manage.store/'=>'门店设置',
		'jy_weishop/verify.index/'=>'普通商品核销',
		'jy_weishop/verify.verifygoods/'=>'计次商品核销',
		'jy_weishop/verify.selfindex/'=>'配送商品核销',
		'jy_weishop/store.manage.count/'=>'核销数据查询',
	);
	$return['status']=array('0'=>'关闭','1'=>'启用');
	$return['store']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回角色列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'角色名称','type'=>'string'),
					array('name'=>'storeid','title'=>'选择所属门店','type'=>'select','options'=>'store'),
					array('type'=>'checkbox','title'=>'选择权限','name'=>'perms','options'=>'perm2','help'=>'设置菜单权限' ),
					array('type'=>'checkbox','title'=>'选择掌上权限','name'=>'perms_mobile','options'=>'perm_mobile','help'=>'设置掌上权限' ),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'name'=>trim($postdata['name']),
		'storeid'=>intval($postdata['storeid']),
		'perms'=>trim($postdata['perms']),
		'perms_mobile'=>trim($postdata['perms_mobile']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_store_role',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_store_role',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_store_role',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的角色！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_store_role',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}